//
//  PVDPhotoView.swift
//  GreenDeal
//
//  Created by Moennig Q on 31/1/2023.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
class PVDPhotoView: View {
    lazy var disposeBag = DisposeBag()
    lazy var scrollView: UIScrollView = DIYScrollView()
    lazy var iconView : UIImageView = UIImageView()
    lazy var showMsgLabel: PaddingLabel = {
       let view = PaddingLabel()
        view.paddingTop = 10
        view.paddingBottom = 10
        view.font = R.font.interSemiBold(size: 15)
        view.textColor = .white
        view.backgroundColor = .clear
        view.numberOfLines = 0
        view.textAlignment = .center
        return view
    }()
    lazy var loadFieldView : LoadFileFailedView = LoadFileFailedView()
    override func setUpView() {
        //scrollView用于缩放图片
        scrollView.delegate = self
        scrollView.isMultipleTouchEnabled = true
        scrollView.isUserInteractionEnabled = true
        scrollView.isScrollEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.backgroundColor = .black
        self.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalTo(self)
            make.height.greaterThanOrEqualTo(0)
        }
        self.iconView.contentMode = UIView.ContentMode.scaleAspectFit
        self.scrollView.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        //
        showMsgLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.vertical)
        showMsgLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.vertical)
        self.addSubview(showMsgLabel)
        showMsgLabel.snp.makeConstraints { make in
            make.top.equalTo(scrollView.snp.bottom)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        self.addSubview(loadFieldView)
        loadFieldView.snp.makeConstraints { make in
            make.edges.equalTo(scrollView)
        }
        loadFieldView.isHidden = true
        showMsgLabel.rx.observe(String.self, "text").subscribe(onNext: {[weak self] (text) in
            guard let strongSelf = self else {return}
            let content = text ?? ""
            strongSelf.showMsgLabel.isHidden = (content == "" ? true : false)
        }).disposed(by: self.disposeBag)
    }
    //加载完图片后，将code imageview显示在scrollview的中间位置,
    func resizeCodeImageView(_ loadFaildFlag: Bool){
        if loadFaildFlag {//加载失败
            self.loadFieldView.isHidden = false
            self.scrollView.isHidden = true
        }else{//加载成功
            self.loadFieldView.isHidden = true
            self.scrollView.isHidden = false
            let imageSize = self.iconView.image!.size
            let scrollViewFrame = scrollView.frame//scrollview的contentSize默认和Framesize一样
            let scaleWidth = scrollViewFrame.size.width / imageSize.width
//            let scaleHeight = scrollViewFrame.size.height / imageSize.height
            
            let minScale: CGFloat = scaleWidth//min(scaleWidth, scaleHeight)
            
            scrollView.maximumZoomScale = minScale * 10
            scrollView.minimumZoomScale = minScale
            scrollView.zoomScale = minScale
            
            self.scrollView.addSubview(iconView)
            //设置图片居中显示
            let x = (self.scrollView.frame.size.width - self.iconView.image!.size.width * minScale) / 2
            let y = (self.scrollView.frame.size.height - self.iconView.image!.size.height * minScale) / 2
            self.iconView.frame = CGRect(x:  x, y: y, width:  self.iconView.image!.size.width * minScale , height: self.iconView.image!.size.height * minScale)
        }
        
    }
}
extension PVDPhotoView: UIScrollViewDelegate{
    //UIScrollViewDelegate
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.iconView
    }
    //解决宽度大于高度的图片放大后，无法滚动到嘴上或最下边的问题
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        let size = scrollView.bounds.size
        let yOffset = max(0, (size.height - iconView.frame.height) / 2)
        iconView.frame.origin.y = yOffset
                
        let xOffset = max(0, (size.width - iconView.frame.width) / 2)
        iconView.frame.origin.x = xOffset
        self.layoutIfNeeded()
    }
    
}
